package com.put.mybills.activities;


import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import com.put.mybills.R;
import com.put.mybills.adapter.GlobalParams;
import com.put.mybills.model.Bill;
import com.put.mybills.model.Facture;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SummaryFragment extends Fragment {
	
	private String factureName;
	
	public SummaryFragment(String factureName) {
		this.factureName = factureName;
	}

	
	private TextView countView;
	private TextView maxView;
	private TextView minView;
	private TextView averageView;
	private TextView savingView;
	private TextView futureBill;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_summary, container, false);
        
        countView = (TextView) rootView.findViewById(R.id.summary_count);
        maxView = (TextView) rootView.findViewById(R.id.summary_max);
        minView = (TextView) rootView.findViewById(R.id.summary_min);
        averageView = (TextView) rootView.findViewById(R.id.summary_average);
        savingView = (TextView) rootView.findViewById(R.id.summary_savings);
        futureBill = (TextView) rootView.findViewById(R.id.summary_future_bill);
        
        
    	Facture facture = GlobalParams.getInstance().facturedatasource.getFacture(factureName);
    	
    	List<Bill> allBills = GlobalParams.getInstance().billdatasource.getAllBills(facture.getId());
    	
    	if (allBills.size() > 0) {
    		
    		Collections.sort(allBills);
    		
    		LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout_summary);
	  		layout.setGravity(Gravity.LEFT);	  
	  		layout.setPadding(15, 15,15, 15);
	  		
	  		Calendar cal = Calendar.getInstance();
	  		int currentDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
	  		
	  		int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	  		//Log.d("DUPA", "Day in month is " + currentDayOfMonth + " all day is " + daysInMonth);
	  		double root = (double)currentDayOfMonth/(double)daysInMonth;
    		
    		int count = 0;
    		double max = Double.MIN_VALUE;
    		double min = Double.MAX_VALUE;
    		double average = 0;
    		double saving = 0;
    		
    		double newPrice = 0;
    		
    		for (int i=0; i<allBills.size();i++){

    			if (allBills.get(i).getPrice() > max)    				
    				max = allBills.get(i).getPrice();
    			
    			if (allBills.get(i).getPrice() < min)    				
    				min = allBills.get(i).getPrice();
    			
    			average += allBills.get(i).getPrice();
    			count++;
    			
    			newPrice += allBills.get(i).getPrice() * root;
	
			}
    		
    		newPrice /= count;
    		newPrice = Math.round(newPrice);
    			
    		futureBill.setText(getResources().getString(R.string.summary_future_bill) + " " + String.valueOf(newPrice)); 		
    		if (allBills.size() > 1)
    		{
    			saving = allBills.get(allBills.size()-1).getPrice() - allBills.get(allBills.size()-2).getPrice();
    			savingView.setText(getResources().getString(R.string.summary_savings) + " " + String.valueOf(saving));
    		}
    		
    		
    		
    		
    		average = Math.round(average / count);  	
    		countView.setText(getResources().getString(R.string.summary_count) + " " + String.valueOf(count));
    		maxView.setText(getResources().getString(R.string.summary_max) + " " + String.valueOf(max) );
    		minView.setText(getResources().getString(R.string.summary_min) + " " + String.valueOf(min));
    		averageView.setText(getResources().getString(R.string.summary_average) + " " + String.valueOf(average));
    		

    	}
    	else
    	{
    		TextView emptyBillList = new TextView(getActivity());
	  		emptyBillList.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
	  		emptyBillList.setText(R.string.facture_summary_data_not_exsist);
	  		LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.layout_summary);
	  		layout.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER);	  
	  		layout.setPadding(30, 30, 30, 30);
	  		layout.removeAllViews();
			layout.addView(emptyBillList);		
    		
    	}
    	
    	
        //android:gravity="center_vertical|center"
        //android:layout_gravity="center_horizontal"
        
        
        
         
        return rootView;
    }

}
